package com.em.common;

import org.apache.commons.lang3.StringUtils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class DateUtils {

    public static Long dateToTimestamp(String date) {
        if (StringUtils.isAnyBlank(date)) {
            return null;
        }
        return toTimestamp(date + " 00:00:00", "yyyy-MM-dd HH:mm:ss");
    }

    public static Long toTimestamp(String date, String formatter) {
        if (StringUtils.isAnyBlank(date, formatter)) {
            return null;
        }
        LocalDateTime dateTime = LocalDateTime.parse(date, DateTimeFormatter.ofPattern(formatter));
        return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    public static String timestampToStr(Long timestamp) {
        return toFotmatter(timestamp, "yyyy-MM-dd HH:mm:ss");
    }

    public static String toFotmatter(Long timestamp, String formatter) {
        if (timestamp == null || StringUtils.isAnyBlank(formatter)) {
            return null;
        }
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());

        return dateTime.format(DateTimeFormatter.ofPattern(formatter));
    }

}
